from django.utils.functional import SimpleLazyObject

from accounts.models import UserNotification


def notification_context(request):
    def get_unread_count():
        if not getattr(request, "user", None) or not request.user.is_authenticated:
            return 0
        return request.user.notifications.filter(read_at__isnull=True).count()

    return {
        "notifications_unread_count": SimpleLazyObject(get_unread_count),
    }
